package com.hgk.wanwan.regist;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.android.volley.VolleyError;
import com.common.dongqiuhui.apps.comm.constants.UrlConstans;
import com.common.dongqiuhui.apps.comm.model.BaseController;
import com.common.dongqiuhui.apps.comm.util.SharedPreferencesUtil;
import com.hgk.common.common.constants.DataConstants;
import com.hgk.common.common.ui.activity.BallWebviewActivity;
import com.hgk.common.common.ui.activity.SimpleBaseActivity;
import com.hgk.common.common.ui.view.RippleView;
import com.hgk.wanwan.login.LoginActivity;
import com.hgk.wanwan.regist.util.TimeCount;

import java.util.HashMap;
import java.util.Map;

import width.hgk.wanwan.R;

public class RegistActivity extends SimpleBaseActivity {

    private final String GET_CODE_URL = UrlConstans.ZWCOMMOMURL + "/domain/user/check_smscode.json";
    private final String GET_REGIST_UIL = UrlConstans.ZWCOMMOMURL+"/domain/user/register.json";
    private final String GET_AGREEMENT_URL = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" ;
    private LinearLayout mLtPhone,mLtCode,mLtLogin;
    private TextView mTvDeal,mTvLogin;//阅读协议
    private EditText mTvCode,mTvPhoneNum;
    private RippleView mRipCode,mRipRegist;
    private ImageView mIvPhoneDel,mIvCodeDel,mIvReDeal;
    private String mCode,mPhoneNum;
    private String code = "0";
    private TimeCount mTimeCount;
    private String mAgreement = "在玩网络服务使用协议";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_regist);
        initView();
        initData();
    }

    private void initView() {
        mLtCode = (LinearLayout) findViewById(R.id.llt_regist_auth_code);
        mLtPhone = (LinearLayout) findViewById(R.id.llt_regist_phone_number);
        mTvDeal = (TextView) findViewById(R.id.tv_regist_deal);
        mTvLogin = (TextView) findViewById(R.id.tv_regist_bottom_login);
        mLtLogin = (LinearLayout) findViewById(R.id.llt_regist_bottom_login);
        mRipCode = (RippleView) findViewById(R.id.rip_regist_get_code);
        mRipRegist = (RippleView) findViewById(R.id.rip_regist_btn);
        mIvPhoneDel = (ImageView) findViewById(R.id.iv_phone_numb_delete);
        mIvCodeDel = (ImageView) findViewById(R.id.iv_regist_paw_delete);
        mTvCode = (EditText) findViewById(R.id.tv_regist_codenum);
        mTvPhoneNum = (EditText) findViewById(R.id.tv_regist_phone_number);
        mIvReDeal = (ImageView) findViewById(R.id.iv_regist_deal);

        mTvLogin.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//设置下划线
        mLtLogin.setOnClickListener(this);
        mTvLogin.setOnClickListener(this);
        mLtCode.setOnClickListener(this);
        mTvDeal.setOnClickListener(this);
        mLtPhone.setOnClickListener(this);
        mIvPhoneDel.setOnClickListener(this);
        mIvCodeDel.setOnClickListener(this);
        mIvReDeal.setOnClickListener(this);

        mRipCode.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                mPhoneNum = mTvPhoneNum.getText().toString();
                if (TextUtils.isEmpty(mPhoneNum)){
                    Toast.makeText(mContext,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                getNetDataForPost("action_code",GET_CODE_URL, BaseController.class);
                mTimeCount = new TimeCount(60000, 1000,mTvCode);
                mTimeCount.start();
            }
        });
        mRipRegist.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                mCode = mTvCode.getText().toString();
                mPhoneNum = mTvPhoneNum.getText().toString();
                if (TextUtils.isEmpty(mPhoneNum)){
                    Toast.makeText(mContext,"手机号不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(mCode)){
                    Toast.makeText(mContext,"请输入验证码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (code.equals("0")){
                    Toast.makeText(mContext,"网络协议需先阅读",Toast.LENGTH_SHORT).show();
                    return;
                }
                getNetDataForPost("action_regist",GET_REGIST_UIL,BaseController.class);
            }
        });
    }
    private void initData() {

    }


    @Override
    protected Map getApiParams(String action) {
        Map<String,String > params = new HashMap<>();
        if(!TextUtils.isEmpty(mPhoneNum)){//
            params.put(DataConstants.TEL_NUMBER,mPhoneNum);
        }
        return null;
    }

    @Override
    protected void success(String action, Object result) {
        if (result==null)return;
        BaseController bean = (BaseController) result;
        if(action.equals("action_code")) {
            Toast.makeText(mContext, "获取验证码成功", Toast.LENGTH_SHORT).show();
        }else if (action.equals("action_regist")){
            Toast.makeText(mContext,"验证成功",Toast.LENGTH_SHORT).show();
            SharedPreferencesUtil.setValue(mContext, DataConstants.TEL_NUMBER, mPhoneNum);
            PsdActivity.launch(mContext,mPhoneNum,mCode);
        }
    }

    @Override
    protected void fail(String action, String errorStr) {
        if (TextUtils.isEmpty(errorStr)){
            errorStr="操作失败";
        }
        if(action.equals("action_code")){
            if(TextUtils.isEmpty(errorStr)){
                errorStr="验证码获取失败，请重新获取";
            }
            mTimeCount.onFinish();
            mTimeCount.cancel();
        }
        Toast.makeText(mContext, errorStr, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void netfail(String action, VolleyError volleyError) {

    }

    @Override
    public void setLeftButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public void setRightButton(RelativeLayout relativeLayout, Button button) {

    }

    @Override
    public Object getTitleString() {
        return "注册";
    }

    @Override
    public int getBodyView() {
        return R.layout.activity_regist;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.llt_regist_auth_code:
                break;
            case R.id.llt_regist_phone_number:
                break;
            case R.id.iv_regist_deal:
                if (code.equals("0")){
                    code="1";
                    mIvReDeal.setImageResource(R.drawable.icon_xuanzhong);
                }else{
                    code="0";
                    mIvReDeal.setImageResource(R.drawable.icon_weixaun_zhong);
                }
                break;
            case R.id.tv_regist_deal:
                //TODO 协议说明（应该是一个HTML网页）
                BallWebviewActivity.launch(mContext,GET_AGREEMENT_URL,mAgreement,null);
                break;
            case R.id.tv_regist_bottom_login:
                LoginActivity.launch(mContext,DataConstants.REGIST_ACTIVITY_VALUE);
                break;
            case R.id.llt_regist_bottom_login:
                break;
            case R.id.iv_phone_numb_delete:
                break;
            case R.id.iv_regist_paw_delete:
                break;
        }
    }
    public static void launch(Context context){
        Intent intent = new Intent(context,RegistActivity.class);
        context.startActivity(intent);
    }
}
